package com.ambow.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ambow.dao.Supplier1Dao;
import com.ambow.dao.SupplierDao;


@WebServlet("/Supplier_Servlet")
public class Supplier_Servlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Supplier_Servlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String ud=request.getParameter("update");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");  
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession  hs = request.getSession(); 
		Supplier1Dao supplierdao=new Supplier1Dao();
		if(ud.equals("supplier_modify")){
			String name = request.getParameter("name");
			String contactman = request.getParameter("contactman");
			String tel = request.getParameter("tel");
			String address = request.getParameter("address");
			String email = request.getParameter("email");

			String remark = request.getParameter("remark");


			
			System.out.println("新name："+name);
			System.out.println("新contactman："+contactman);
			System.out.println("新tel："+tel);
			System.out.println("新address："+address);
			System.out.println("新email："+email);
			System.out.println("新remark："+remark);
			int supplier_id=(Integer.parseInt( request.getParameter("id")));


			
			boolean f=supplierdao.updateinfo(supplier_id,name, contactman,tel,address,email,remark);
			if(f==true){
//				response.sendRedirect(request.getContextPath()+"/Person_showServlet?val=personinfo");
				out.print("信息修改成功！下次登录查看。");
			}
			
		}
		else if(ud.equals("supplier_add")){
			
			String new_name = request.getParameter("name");
			
			String new_man = request.getParameter("man");
//			byte [] b=new_name.getBytes("iso-8859-1");
//			new_name=new String(b,"utf-8");
			
			String new_tel = request.getParameter("tel");
			
			String new_address = request.getParameter("address");
			
			String new_mail = request.getParameter("mail");
//			byte [] b1=new_postion.getBytes("iso-8859-1");
//			new_postion=new String(b1,"utf-8");
			
			
			String new_remark = request.getParameter("notes");
//			byte [] b3=new_remark.getBytes("iso-8859-1");
//			new_remark=new String(b3,"utf-8");
			
			System.out.println(new_name);
			System.out.println(new_man);
			System.out.println(new_tel);
			System.out.println(new_address);
			System.out.println(new_mail);
			System.out.println(new_remark);
			
			boolean f = supplierdao.addSupplier(new_name, new_man, new_tel, new_address,new_mail, new_remark);
			
			if(f==true){
				out.print("添加成功！");
			}
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
